package com.taysear.dbservice.resources;


import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Random;
/**
 * 
 * @author sharif
 *
 */
public class CommonTasks 
{
	
	/**
	 * Gets LowestValue in given digit range
	 * 
	 * @param n
	 * @return int
	 */
	public static int getLDV(int n)
	{
		String str="1";		
		for(int i=1; i<=(n-1);str+="0",i++);
		
		return Integer.parseInt(str);
	}
	/**
	 * Generates Key of a specified length
	 * 
	 * @param pNdigits
	 * @return String
	 */
	public static String generateKey(int pNdigits)
	{
		Random rand=new Random();
		int min=getLDV(pNdigits);
		int max=getLDV(pNdigits+1)-1;
		int randomNum = rand.nextInt((max - min) + 1) + min;

		return Integer.toString(randomNum);
	}
	public static String getNewPwd()
	{
		return "P"+generateKey(4);
	}
	 public static String getMacAddr() 
	 {
		   String macAddr= ""; 
		    InetAddress addr;
		 try {
		  addr = InetAddress.getLocalHost();

		        System.out.println(addr.getHostAddress());
		        NetworkInterface dir = NetworkInterface.getByInetAddress(addr);
		        byte[] dirMac = dir.getHardwareAddress();

		        int count=0;
		        for (int b:dirMac){
		         if (b<0) b=256+b;
		         if (b==0) {
		               macAddr=macAddr.concat("00"); 
		         }
		         if (b>0){

		          int a=b/16;
		          if (a==10) macAddr=macAddr.concat("A");
		          else if (a==11) macAddr=macAddr.concat("B");
		          else if (a==12) macAddr=macAddr.concat("C");
		          else if (a==13) macAddr=macAddr.concat("D");
		          else if (a==14) macAddr=macAddr.concat("E");
		          else if (a==15) macAddr=macAddr.concat("F");
		          else macAddr=macAddr.concat(String.valueOf(a));
		             a = (b%16);
		          if (a==10) macAddr=macAddr.concat("A");
		          else if (a==11) macAddr=macAddr.concat("B");
		          else if (a==12) macAddr=macAddr.concat("C");
		          else if (a==13) macAddr=macAddr.concat("D");
		          else if (a==14) macAddr=macAddr.concat("E");
		          else if (a==15) macAddr=macAddr.concat("F");
		          else macAddr=macAddr.concat(String.valueOf(a));
		         }
		         if (count<dirMac.length-1)macAddr=macAddr.concat("-");
		         count++;
		        }

		 } catch (UnknownHostException e) {
		  // TODO Auto-generated catch block
		  macAddr=e.getMessage();
		 } catch (SocketException e) {
		  // TODO Auto-generated catch block
		  macAddr = e.getMessage();
		 }
		 return macAddr;
		 }

}

